package com.quasar.hpatient.view.adapter;

import android.text.style.ForegroundColorSpan;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.makeramen.roundedimageview.RoundedImageView;
import com.quasar.hpatient.MainAppliaction;
import com.quasar.hpatient.R;
import com.quasar.hpatient.model.medicalmodel.FriendsData;
import com.quasar.hpatient.network.RetrofitConnection;
import com.quasar.hpatient.utils.Spanny;
import com.quasar.hpatient.utils.TimeZoneUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * author : 许建宏
 * Date: 2017/10/13 0013
 * View:我的好友列表
 */

public class FriendsListAdapter extends BaseQuickAdapter<FriendsData, BaseViewHolder> {

    public FriendsListAdapter() {
        super(R.layout.friend_item);
    }

    @Override
    protected void convert(BaseViewHolder helper, FriendsData item) {
        //真实姓名
        if (item.getRealName() != null) {
            helper.setText(R.id.tv_name, item.getRealName());
        }

        String dayTime = null;
        if (item.getBirthday() != null && item.getBirthday().length() > 0) {

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = null;
            try {
                date = sdf.parse(item.getBirthday());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            int age = TimeZoneUtil.getAge(date);
            dayTime = age + "";
        } else {
            dayTime = "未知";
        }

        Spanny spanny = new Spanny();
        spanny.append("年龄：", new ForegroundColorSpan(color(R.color.btcolor)));
        spanny.append(dayTime, new ForegroundColorSpan(color(R.color.textcolor)));
        helper.setText(R.id.tv_age, spanny);

        RoundedImageView roundedImageView = helper.getView(R.id.headimg);
        if (item.getCover() != null && item.getCover().length() > 0) {
            Glide.with(mContext).load(RetrofitConnection.PHOTOURL + item.getCover()).into(roundedImageView);
        }else{
            Glide.with(mContext).load(R.mipmap.nopic).into(roundedImageView);
        }
        //拒绝
        helper.addOnClickListener(R.id.ll_frient_item);

    }

    private int color(int value) {
        return MainAppliaction.getInstance().getResources().getColor(value);
    }
}
